package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.HookBeanRes;
import com.hengxinguotong.zhihuichengjian.fragment.HookMonitorFragment;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 吊钩监测
 * Created by Administrator on 2017/9/14.
 */

public class HookMonitorActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tv_back)
    HXTextView tvBack;

    private List<String> mTitleDataList;
    private ArrayList<BaseFragment> fragments;
    private DeviceListPagerAdapter hookListPagerAdapter;
    private int currentPage = 0;
    private HookMonitorActivity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hook_monitor);
        ButterKnife.bind(this);
        mActivity = this;
        getHookInfo("加载中…");
    }

    private void getHookInfo(String alert) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(mActivity,"constructionid"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, mActivity, "/hook/getHookInfo/" + SPUtil.getString(mActivity,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    HookBeanRes beanRes = gson.fromJson(json, HookBeanRes.class);
                    if(beanRes.getStatus() == 1){
                        if(beanRes.getValue() == null || beanRes.getValue().getList() == null){
                            showToast("监控列表为空");
                        }else {
                            process(beanRes);
                        }
                    }else {
                        showToast(beanRes.getMsg());
                    }
                }
            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    private void process(HookBeanRes beanRes) {
        List<HookBeanRes.HookValue.HookBean> list;
        String nvrStr = beanRes.getValue().getNvrStr();
        if(beanRes.getValue() == null || beanRes.getValue().getList() == null){
            list = new ArrayList<>();
        }else {
            list = beanRes.getValue().getList();
        }
        viewPager.setOffscreenPageLimit(list.size());
        fragments = new ArrayList<BaseFragment>();
        mTitleDataList = new ArrayList<String>();
        for(int i=0; i<list.size(); i++){
            mTitleDataList.add(list.get(i).getName());
            fragments.add(HookMonitorFragment.newInstance(list.get(i), nvrStr));
        }
        hookListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(hookListPagerAdapter);

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_adadad));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_ffc065));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        currentPage = index;
                        viewPager.setCurrentItem(currentPage);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_ffc065));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(mActivity, 40);
            }
        });

        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(2.0f));
        fragmentContainerHelper.setDuration(300);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
                currentPage = position;

            }
        });
        currentPage = 0;
        viewPager.setCurrentItem(currentPage);
    }

    @OnClick({R.id.tv_back})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
        }
    }
}
